package com.one.blocks.rbac.jetcache;

import com.alicp.jetcache.support.AbstractValueEncoder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author linfuxin Created on 2022-02-14 14:13:32
 */
public class JacksonValueEncoder extends AbstractValueEncoder {

    private final ObjectMapper objectMapper;

    public JacksonValueEncoder(ObjectMapper objectMapper) {
        super(false);
        this.objectMapper = objectMapper;
    }

    @Override
    public byte[] apply(Object o) {
        try {
            return objectMapper.writeValueAsBytes(o);
        } catch (JsonProcessingException ex) {
            throw new RuntimeException("Could not write JSON: " + ex.getMessage(), ex);
        }
    }
}
